package com.ants.weixin.web.common;

import com.ants.platform.core.bean.Page;
import com.ants.platform.core.plugins.EhCacheClient;
import com.ants.platform.core.utils.ResponseUtil;
import com.ants.platform.core.validator.SpringValidators;
import com.ants.platform.weixin.model.User;
import com.ants.platform.weixin.service.TestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by liushun on 2016/8/9.
 */
@Controller
public class IndexController {

    @Autowired
    private TestService testService;

    @Autowired
    private EhCacheClient ehCacheClient;

    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    @ResponseBody
    public Object getUsers(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize, @Validated User user, BindingResult result) {
        String erroMsg = SpringValidators.validate(result);
        //校验数据是否为空
        if (StringUtils.isNoneEmpty(erroMsg)) return ResponseUtil.jsonFail(erroMsg);
        long startTime = System.currentTimeMillis();
        Page page = testService.getList(pageNum, pageSize, null, null);
        return ResponseUtil.jsonSuccess(page, startTime);
    }

    @RequestMapping(value = "/keys", method = RequestMethod.GET)
    @ResponseBody
    public Object keys() {
        List<String> baseCache = ehCacheClient.getKeys("baseCache");
        return ResponseUtil.jsonSuccess(baseCache);
    }

    @RequestMapping(value = "/tx", method = RequestMethod.GET)
    @ResponseBody
    public Object tx() {
        testService.testTx();
        return "ok";
    }


    @RequestMapping(value = "/ftl", method = RequestMethod.GET)
    public ModelAndView ftl() {
        return new ModelAndView("test");
    }
}
